package com.xkcyy.one.controller;

import com.xkcyy.one.common.Result;
import com.xkcyy.one.core.security.SysIdentity;
import com.xkcyy.one.dto.ConversationCreateDTO;
import com.xkcyy.one.dto.ConversationDTO;
import com.xkcyy.one.dto.ConversationUpdateDTO;
import com.xkcyy.one.entity.Conversation;
import com.xkcyy.one.service.ConversationService;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 对话控制器
 *
 * @author AI Assistant
 */
@RestController
@RequestMapping("/api/conversations/")
@AllArgsConstructor
public class ConversationController {
    
    private final ConversationService conversationService;
    
    /**
     * 获取所有对话
     *
     * @return 对话列表
     */
    @GetMapping("/list")
    public Result<List<ConversationDTO>> getAllConversations() {
        return Result.success(conversationService.getAllConversations());
    }
    
    /**
     * 根据ID获取对话
     *
     * @param id 对话ID
     * @return 对话信息
     */
    @GetMapping("/get")
    public Result<ConversationDTO> getConversationById(@RequestParam Long id) {
        return Result.success(conversationService.getConversationById(id));
    }
    
    /**
     * 根据用户ID获取对话列表
     *
     * @param userId 用户ID
     * @return 对话列表
     */
    @GetMapping("/user")
    public Result<List<ConversationDTO>> getConversationsByUserId(@RequestParam Long userId) {
        return Result.success(conversationService.getConversationsByUserId(userId));
    }
    
    /**
     * 创建对话
     *
     * @param createDTO 对话创建信息
     * @return 创建后的对话信息
     */
    @PostMapping("/create")
    public Result<ConversationDTO> create(@RequestBody ConversationCreateDTO createDTO) {
        createDTO.setUserId(SysIdentity.current().getId());
        return Result.success(conversationService.createConversation(createDTO.toEntity()));
    }
    
    /**
     * 更新对话
     *
     * @param id 对话ID
     * @param updateDTO 对话更新信息
     * @return 更新后的对话信息
     */
    @PutMapping("/update")
    public Result<ConversationDTO> updateConversation(@RequestParam Long id, @RequestBody ConversationUpdateDTO updateDTO) {
        Conversation conversation = updateDTO.toEntity();
        conversation.setId(id);
        return Result.success(conversationService.updateConversation(conversation));
    }
    
    /**
     * 删除对话
     *
     * @param id 对话ID
     * @return 操作结果
     */
    @DeleteMapping("/delete")
    public Result<Void> deleteConversation(@RequestParam Long id) {
        conversationService.deleteConversation(id);
        return Result.success();
    }
}
